<?php

class Headuser extends MY_Controller
{
    function __construct()
    {
        parent::__construct();

        $this->load->model("user");
    }

    function add()
    {
        $data = $this->input->post();
        if($data){

            if ($data['password'] && !empty($data['password']) ) {
                $data['passwd'] = $data['password'];
            }else{
                $str = substr(md5(time()),0,6);
                $data['passwd'] = md5($str);
            }

            $data['title'] = $data['level'] = $data['role'];

            unset($data['role']);
            unset($data['password']);
            unset($data['sso']);
            unset($data['sessionKey']);
            $data['role_id'] = '4';
            $data['sms_warning_level'] = '4';
            $data['email_warning_level'] = '4';
            $data['branch_id'] = 0;
            $data['information'] = 0;
            $data['ftime'] = time();
            $data['verify_validity'] = 0;
            $data['verify_code'] = '';
            $data['verify_time'] = 0;
            $data['employment_date'] = date('Y-m-d');

            $data['datacenter_id'] = $this->_user['datacenter_id'];

            $data['owner_id'] = $this->_user['owner_id'];

            $this->db->insert('sys_staff', $data);
            $user_id = $this->db->insert_id();

            $this->logop("sys_staff");

            if ($user_id) {
		$menu_list = $this->db->query('select `id` from `sys_menu`')->result();
		foreach($menu_list as $menu) {
		    $menu_data = array(
			'staff_id' => $user_id,
			'task_id' => $menu->id
		    );

		    $this->db->insert('sys_rights', $menu_data);
		}
                $this->allocatePermition($user_id, $data['role_id']);
                $this->setDefaultIcon($user_id);//默认图标
                echo "<script>alert('创建用户成功!');</script>";
            }else{
                echo "<script>alert('创建用户失败!');</script>";
            }
        }
    }

    function edit()
    {
        $data = $this->input->post();
        if($data){
            if ($data['password'] && !empty($data['password']) ) {
                $data['passwd'] = $data['password'];
            }

            $data['title'] = $data['level'] = $data['role'];

            unset($data['role']);
            unset($data['password']);
            unset($data['sso']);
            unset($data['sessionKey']);

            $data['datacenter_id'] = $this->_user['datacenter_id'];

            $data['owner_id'] = $this->_user['owner_id'];

            $this->user->modify($data);
        }
    }

    function getUserId() {
        $data = $this->input->post();

        $user = $this->db->query('select `id` from `sys_staff` where `email` = "'.$data['email'].'" LIMIT 1')->row_array();

        echo json_encode($user);
        exit();
    }

    //权限编辑操作
    function editPermit($userid)
    {
        $data = $this->input->post();
        $this->load->model("sysrights");

        if($data){
            //var_dump($data);exit;
            $this->logop("sys_staff");
            $this->sysrights->permitModify($userid, $data['data']);
            redirect(site_url("/users/"));
        }
        else{
            $allmenus = $this->sysrights->getAllUserRights($userid);//getrights
            //unset($allmenus[32]);  //配置管理
            //unset($allmenus[166]); //系统配置
            $data['allmenus'] = $allmenus;
            //var_dump( $data['allmenus'] );exit;
            $this->load->view('template/user/role_edit.html',$data);
        }
    }

    //冻结用户,0冻结，1解冻
    function freeze($uid,$flag=0)
    {
        if($this->_user['role_id'] == 4){
            echo '<meta charset="UTF-8" />';
            echo "<script>alert('权限不足!');history.go(-1);</script>";

        }

        $data['id'] = $uid;
        $data['ftime'] = $flag ? " " : date("Y-m-d");
        //var_dump($data);exit;

        $ret = $this->user->modify($data);
        $this->logop("sys_staff");
        redirect(site_url('users/index'));
    }


    //编辑用户信息
    function edituser($uid)
    {

        $post = $_POST;
        if($post) {
            if ($_POST['pwd'] && !empty($_POST['pwd']) ) {
                $data['passwd'] = md5($_POST['pwd']);
            }
            //$title0 = $data['title0'];
            if(isset($post['userid']) && !empty($post['userid']) ){
                $data['id'] = $post['userid'];
            }
            if(isset($post['owner_id']) && !empty($post['owner_id']) ){
                $data['owner_id'] = $post['owner_id'];
            }
            if(isset($post['datacenter_id']) && !empty($post['datacenter_id']) ){
                $data['datacenter_id'] = $post['datacenter_id'];
            }
            if(isset($post['role_id']) && !empty($post['role_id']) ){
                $data['role_id'] = $post['role_id'];
                $role = $this->db->get_where('sys_group',array('id'=>$data['role_id']))->result_array();
                if(isset($role[0]['title']))
                    $data['level'] = $role[0]['title'];
            }
            if(isset($post['depart_id']) && !empty($post['depart_id']) ){
                $data['depart_id'] = $post['depart_id'];
            }
            if(isset($post['code']) && !empty($post['code']) ){
                $data['code'] = $post['code'];
            }
            if(isset($post['name']) && !empty($post['name']) ){
                $data['name'] = $post['name'];
            }
            if(isset($post['email']) && !empty($post['email']) ){
                $data['email'] = $post['email'];
            }
            if(isset($post['qq']) && !empty($post['qq']) ){
                $data['qq'] = $post['qq'];
            }
            if(isset($post['sex']) && !empty($post['sex']) ){
                $data['sex'] = $post['sex'];
            }
            if(isset($post['mobile']) && !empty($post['mobile']) ){
                $data['mobile'] = $post['mobile'];
            }
            if(isset($post['sms_warning_level']) && !empty($post['sms_warning_level']) ){
                $data['sms_warning_level'] = $post['sms_warning_level'];
            }
            if(isset($post['email_warning_level']) && !empty($post['email_warning_level']) ){
                $data['email_warning_level'] = $post['email_warning_level'];
            }

            $userid = $this->user->modify($data);
            $this->logop("sys_staff");
            if ($userid) {
                echo '<meta charset="UTF-8" />';
                echo "<script>alert('更新用户成功!');document.URL=location.href;</script>";
            }else{
                echo '<meta charset="UTF-8" />';
                echo "<script>alert('更新用户失败!');history.go(-1);</script>";
            }

        }else{
            $data['user'] = $this->user->getById($uid);
            if( empty($data['user']) ){
                echo '<meta charset="UTF-8" />';
                echo "$uid<script>alert('用户信息不存在!');history.go(-1);</script>";
                exit;
            }

            $this->load->model("depart");
            $depart = $this->depart->getDepartName( $data['user']['owner_id'] );
            foreach($depart as $k=>$v){
                $arr_depart[$k] = $v['name'];
            }
            $data['depart'] = $arr_depart;
            $title = $this->depart->getNameByOwnerId( $data['user']['owner_id'] );
            foreach($title as $k=>$v){
                $arr_title[$k] = $v['title'];
            }
            $data['title'] =$arr_title;


            $this->load->model("group");
            $this->load->model("datacenter");
            $this->load->model("owner");
            //$data['gs'] = $this->group->getAll2();
            $data['gs'] = $this->group->getAll2($this->_user['role_id']);
            $data['dcs'] = $this->datacenter->getAll2();
            $data['owner'] = $this->owner->getAll2();
            $this->load->helper('form');
            $this->load->view('template/user/edit.html',$data);
        }

    }
}

